import 'package:course_control/utils/fallbackcup.dart';
import 'package:flutter/services.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_utils/flutter_utils.dart';
import 'package:get/get.dart';
import 'package:oktoast/oktoast.dart';
import 'app/routes/app_pages.dart';
import 'config/com_header.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  var onError = FlutterError.onError; //先将 onerror 保存起来
  FlutterError.onError = (FlutterErrorDetails details) {
    onError?.call(details); //调用默认的onError
    LogUtil.logE(details.toString());
  };
  await initLogger();
  var logPath = await LogPath.getLoggerDir();
  LogUtil.logD(logPath);
  await SystemChrome.setEnabledSystemUIMode(SystemUiMode.manual, overlays: []);
  await Future.delayed(const Duration(seconds: 1));
  Devices.init(width: 1280, height: 800);

  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});
  @override
  State<StatefulWidget> createState() => MyAppState();
}

class MyAppState extends State<MyApp> with WidgetsBindingObserver {
  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addObserver(this);
  }

  @override
  void didChangeMetrics() {
    Devices.init(width: 1280, height: 800);
    super.didChangeMetrics();
  }

  @override
  void dispose() {
    WidgetsBinding.instance.removeObserver(this);
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return OKToast(
      child: GetMaterialApp(
          title: "Application",
          initialRoute: AppPages.INITIAL,
          getPages: AppPages.routes,
          debugShowCheckedModeBanner: false,
          localizationsDelegates: const [
            // RefreshLocalizations.delegate,
            GlobalWidgetsLocalizations.delegate,
            GlobalMaterialLocalizations.delegate,
            DefaultCupertinoLocalizations.delegate,
            CupertinoLocalizationsDelegate(),
          ],
          supportedLocales: const [
            Locale("zh", "CH"),
            Locale("en", "US"),
          ],
          localeResolutionCallback:
              (Locale? locale, Iterable<Locale> supportedLocales) {
            print("change language");
            return locale;
          },
          builder: (context, Widget? widget) {
            return MediaQuery(
              ///设置文字大小不随系统设置改变
              data: MediaQuery.of(context).copyWith(textScaleFactor: 1.0),
              child: FlutterEasyLoading(
                child: widget,
              ),
            );
          }),
    );
  }
}
